*** Estimate Markups Baseline
*Bottom-Up Markup flucatuations (Burstein, Carvalho, Grassi)






**************** Preamble **************** *************************************


* housekeeping
clear all 
macro drop _all
set more off, permanently 

* set working directory 
cd "C:\Users\Public\Documents\BCG_DGM\BCG\replication_file_jan25_package\"



************************ Load data
use data/ficusfare_reduced_9419_sec2_year2_firm_va_sevyears_v0222, clear 



************** Generate and rename variables ***********************************

*sale
gen s = ln(catotalR)
gen sale = catotalR

*materials + merchandise
gen m = ln(acha4R)

*labor
gen v = ln(salR)

*capital
gen k = ln(immocorR)

*service
gen o = ln(autachaR)

*Industry list
local indlist 08 13 14 15 16 17 18 20 22 23 24 25 26 27 28 29 30 31 32 33 43 46 70 95

*price
gen p = ln(price_firm_std8_m2345_wm_wm)


*quantity
gen q = log(catotalR) - p



*summary stats
su s m v k o p q ms5d



************** Winsorised Input Variables **************************************

*code the winsorizing by sector
local winlevel 01
foreach var in  s m v k o p q {
	gen `var'_w = .
	foreach ind in  `indlist' {
	
		winsor `var' if naf2d_num==`ind', p(.`winlevel') gen(`var'_temp)
		replace `var'_w  = `var'_temp if naf2d_num==`ind'
		drop `var'_temp
	
	}
	drop `var'
	rename `var'_w `var'
	label var `var' "`var' win `winlevel'"
}

************* summary stats **************************************************

su s q p v k o m  if (naf2d_num~=70 & naf2d_num~=95)



************** Save Data for Python ********************************************

preserve
	

		keep firmsId year  naf2d_num sale  v k m o s p q ms5d 
		order firmsId year  naf2d_num sale  v k m o s p q ms5d
		
		xtset firmsId year
		sort firmsId year	
		sort naf2d_num firmsId year
		
		save "data\data_smallest_for_python_baseline.dta", replace

	
restore


****** RUN THE PYTHON FILE DIRECTLY
************** Run the Python Routine ******************************************
*quantity estimate
shell "C:\Users\Public\Documents\WPy64-3740\python-3.7.4.amd64\python.exe" ///
			"C:\Users\Public\Documents\BCG_DGM\BCG\replication_file_jan25_package\markup_estim_pyhton\run_for_stata_FSQ_baseline.py" 

*revenue estimate			
shell "C:\Users\Public\Documents\WPy64-3740\python-3.7.4.amd64\python.exe" ///
			"C:\Users\Public\Documents\BCG_DGM\BCG\replication_file_jan25_package\markup_estim_pyhton\run_for_stata_BFSR_baseline.py" 

	

